<template>
  <a-form class="smart-query-form" v-privilege="'oa:enterprise:query'">
    <a-row class="smart-query-form-row">
      <a-form-item label="登录账号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.username" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item label="姓名" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.displayName" placeholder="请输入描述" />
      </a-form-item>
      <a-form-item label="员工编号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.employeeNumber" placeholder="请输入描述" />
      </a-form-item>
      <a-form-item label="开始时间" class="smart-query-form-item">
        <a-date-picker
          show-time
          placeholder="选择开始时间"
          @change="onChange"
          v-model:value="queryForm.startDate"
          valueFormat="YYYY-MM-DD HH:mm:ss"
          @ok="onOk"
        />
      </a-form-item>
      <a-form-item label="结束时间" class="smart-query-form-item">
        <a-date-picker
          show-time
          placeholder="选择结束时间"
          @change="onChange"
          v-model:value="queryForm.endDate"
          valueFormat="YYYY-MM-DD HH:mm:ss"
          @ok="onOk"
        />
      </a-form-item>
      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button type="primary" danger @click="OverClick">终止</a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.APPLICATIONMANAGEMENT.ID" :refresh="ajaxQuery" />
      </div>
    </a-row>
    <a-table
      size="small"
      rowKey="id"
      :dataSource="tableData"
      :columns="columns"
      :pagination="false"
      :loading="tableLoading"
      bordered
      :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
    >
      <template #bodyCell="{ column, record, text, index }">
        <template v-if="column.dataIndex === 'index'">
          {{ index }}
        </template>
        <template v-if="column.dataIndex === 'sessionStatus'">
          <span v-if="record.sessionStatus == 1">
            <check-circle-two-tone />
          </span>
          <span v-else> </span>
        </template>
        <template v-if="column.dataIndex === 'iconBase64'">
          <img :src="record.iconBase64" alt="" style="width: 40px" />
        </template>
        <template v-if="column.dataIndex === 'status'">
          <a-switch v-model:checked="record.status" :checkedValue="1" :unCheckedValue="0" :disabled="true">
            <template #checkedChildren><check-outlined /></template>
            <template #unCheckedChildren><close-outlined /></template>
          </a-switch>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNumber"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { useRouter } from 'vue-router';
import { smartSentry } from '/@/lib/smart-sentry';
import { defaultTimeRanges } from '/@/lib/default-time-ranges';
import TableOperator from '/@/components/support/table-operator/index.vue';
import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
import { permissionsApi } from '/@/api/access/permissions';
import dayjs from 'dayjs';

// --------------------------- 表格 列 ---------------------------

const columns = ref([
  {
    title: '会话标志',
    dataIndex: 'sessionId',
  },
  {
    title: '登录名',
    dataIndex: 'username',
  },
  {
    title: '姓名',
    dataIndex: 'displayName',
  },
  {
    title: '状态',
    dataIndex: 'sessionStatus',
  },
  {
    title: '登录方式',
    dataIndex: 'loginType',
  },
  {
    title: '访问地址',
    dataIndex: 'sourceIp',
  },
  {
    title: '浏览器',
    dataIndex: 'browser',
  },
  {
    title: '平台',
    dataIndex: 'platform',
  },
  {
    title: '登录时间',
    dataIndex: 'loginTime',
  },
]);

// --------------------------- 查询 ---------------------------

const queryFormState = {
  username: '',
  displayName: '',
  employeeNumber: '',
  startDate: dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
  endDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  pageSize: '',
  startDatePicker: 1730079686585,
  endDatePicker: 1732671686585,
  pageNumber: 1,
  pageSize: PAGE_SIZE,
  pageSizeOptions: 50,
};
const queryForm = reactive({ ...queryFormState });
const tableLoading = ref(false);
const tableData = ref([]);
const total = ref(0);

// 日期选择
const searchDate = ref();
const onChange = (value, dateString) => {
  console.log(value);

  // queryForm.endDate = queryForm.endDate.format('YYYY-MM-DD HH:mm:ss');
  // queryForm.startDate = queryForm.startDate.format('YYYY-MM-DD HH:mm:ss');
};
const onOk = (value) => {};

// 查询
function onSearch() {
  queryForm.pageNumber = 1;
  ajaxQuery();
}
// 重置查询
function resetQuery() {
  searchDate.value = [];
  Object.assign(queryForm, queryFormState);
  ajaxQuery();
}

// 列表数据获取
async function ajaxQuery() {
  try {
    let responseModel = await permissionsApi.getSession(queryForm);
    const list = responseModel.data.rows;
    total.value = responseModel.data.records;
    tableData.value = list;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

// --------------------------- 终止 ---------------------------

let router = useRouter();

async function OverClick() {
  try {
    let ids = Object.values(selectedRowKeyList.value).join(',') + ',';
    let res = await permissionsApi.Overterminate(ids);
    if (res.code === 0) {
      message.success('终止成功！');
    } else {
      message.error('终止失败！');
    }
    ajaxQuery();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys;
}
onMounted(() => {
  ajaxQuery();
});
</script>